//
// Created by 28365 on 2025/3/5.
//

#include <mutex>
#include "../include/config/log/BasisLogger.h"

bool BasisLogger::isEnabled() const {
    return enabled;
}

void BasisLogger::setEnabled( bool _enabled) {
    BasisLogger::enabled = _enabled;
}

bool BasisLogger::isDebug() const {
    return _debug;
}

void BasisLogger::setDebug(bool isDebug) {
    BasisLogger::_debug = isDebug;
}

void BasisLogger::info(const string &message) const{
    sout((string)"[INFO] " + message);
}

void BasisLogger::warn(const string &message) const {
    sout((string)"[WARN] " + message);
}

void BasisLogger::debug(const string &message) const {
    if (BasisLogger::_debug){
        sout((string)"[DEBUG] " + message);
    }
}

void BasisLogger::error(const string &message, const exception &exception) const {
    sout((string)"[ERROR] " + message + (string)"\n[EXCEPTION]" + exception.what());
}

void BasisLogger::error(const string &message) const {
    sout((string)"[ERROR] " + message);
}

void BasisLogger::warn(const string &message, const exception& exception) const {
    sout((string)"[WARN] " + message + (string)"\n[EXCEPTION]" + exception.what());
}

std::mutex logMutex;

void BasisLogger::sout(const string &message) const {
    if (BasisLogger::enabled){
        std::lock_guard<std::mutex> lock(logMutex);
        time_t now = time(nullptr);
        cout << put_time(localtime(&now), "[%Y-%m-%d %H:%M:%S]") << message << endl;
    }
}
